package com.deepglint.window;

import com.deepglint.beans.SensorReading;
import org.apache.flink.api.common.eventtime.*;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.AscendingTimestampExtractor;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.OutputTag;

/**
 * @author mj
 * @version 1.0
 * @date 2021-11-18 23:46
 */
public class WindowTest_EventTime {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
//        env.setStreamTimeCharacteristic(); // 1.12开始，默认的就是eventTime
        env.getConfig().setAutoWatermarkInterval(200);// 设置周期性的waterMark的周期时间

        DataStream<String> streamSource = env.socketTextStream("192.168.150.128", 7777);

        DataStream<SensorReading> dataStream = streamSource.map(line -> {
                    String[] split = line.split(",");
                    return new SensorReading(split[0], split[1], new Long(split[2]), new Double(split[3]));
                })
//                // 升序数据设置事件时间(返回的时间单位为毫秒)
//                .assignTimestampsAndWatermarks(new AscendingTimestampExtractor<SensorReading>() {
//                    @Override
//                    public long extractAscendingTimestamp(SensorReading element) {
//                        return element.getTimestamp() * 1000L;
//                    }
//                })
                // 乱序处理(返回的时间单位为毫秒)
                .assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<SensorReading>(Time.seconds(2)) {
                    @Override
                    public long extractTimestamp(SensorReading element) {
                        return element.getTimestamp() * 1000L;
                    }
                })
                // 乱序处理,最新的方式
//                .assignTimestampsAndWatermarks(new WatermarkStrategy<SensorReading>() {
//                    @Override
//                    public WatermarkGenerator<SensorReading> createWatermarkGenerator(WatermarkGeneratorSupplier.Context context) {
//                        return new WatermarkGenerator<SensorReading>() {
//                            @Override
//                            public void onEvent(SensorReading event, long eventTimestamp, WatermarkOutput output) {
//                                output.emitWatermark(new Watermark(event.getTimestamp() * 1000L));
//                            }
//
//                            @Override
//                            public void onPeriodicEmit(WatermarkOutput output) {
//                                output.emitWatermark(new Watermark(1));
//                            }
//                        };
//                    }
//                })
                ;

        OutputTag<SensorReading> outputTag = new OutputTag<>("sensor");
        SingleOutputStreamOperator<SensorReading> minTemStream = dataStream.
                keyBy("id").
                window(TumblingEventTimeWindows.of(Time.seconds(15)))
                .allowedLateness(Time.seconds(1)) // 允许数据迟到时间
                .sideOutputLateData(outputTag) // 迟到数据，侧输出流
                .minBy("temperature");

        minTemStream.print("min");
        minTemStream.getSideOutput(outputTag).print("sensor");

        env.execute();
    }
}
